from calc_manager.base.plugins import PluginObject
import requests
from dataclasses import dataclass


@dataclass
class Material:
    id: int
    name: str
    card_id: int
    elasticModulus: float
    shearModulus: float
    poissonRatio: float
    density: float
    thermExpanCoeff: float
    tensionStressLimit: float
    compressStressLimit: float
    shearStressLimit: float


class MaterialPlugin(PluginObject):
    def __init__(self, data_engine_endpoint, data_engine_api_token):
        self.data_engine_endpoint = data_engine_endpoint
        self.api_token = data_engine_api_token
        print("material plugin init......")

    # 根据材料名称获取材料信息
    def get_by_name(self, name: str) -> Material:
        url = f'{self.data_engine_endpoint}/api/v1/restData/data-engine/material/getByName?apiToken={self.api_token}'
        try:
            rep = requests.get(url=url, params={"name": name})
            rep.raise_for_status()
            res = rep.json()
            if res['status'] != 200:
                print(f"get material by name failed, res = {res}")
                return
            if len(res['data']) > 0:
                return Material(**res['data'][0])
        except Exception as e:
            print(f"get material by name error, url = {url}, name = {name}")
            print(e)
            return

    # 根据材料id获取材料信息
    def get_by_mid(self, mid: int):
        return